package com.xcity.game.adventure;

import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.adventure.model.ActionTarget;
import com.xcity.game.adventure.model.ActionTargetQueue;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.buildv2.BuildV2.State;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.player.robot.RobotUtils;
import com.xcity.game.relation.RelationService;

import naga.x.App;
import naga.x.util.CommonUtils;

public class AdventureUtils {

	public static int[][] stealGoldRegions = new int[][] {
//		玩家1-16级，偷窃金额1-5w之间随机
		{ 10000, 50000 },
//		17-25级偷窃金额6-13w之间浮动
		{ 60000, 130000 },
//		26-44级偷窃金额16-28w之间浮动
		{ 160000, 280000 },
//		45-60级偷窃金额26-34w浮动
		{ 260000, 340000 },
	};
	
	// 每天最多被偷/砸次数
	public static int maxCountBeTarget = 20;
	// 保护等级
	public static int protectionLevel = 10;
	// 保护现金
	public static int protectionGold = 20000;
	
	public static int[] getStealGoldRegion(Player source) {
		int level = source.getLevel();
		if (level < 17) {
			return stealGoldRegions[0];
		}
		if (level < 26) {
			return stealGoldRegions[1];
		}
		if (level < 45) {
			return stealGoldRegions[2];
		}
		return stealGoldRegions[3];
	}
	
	/**
	 * 修正偷取目标的金额
	 * @param target
	 */
	public static void fixStealTargetGold(ActionTarget target) {
		if (target.getGold() < 100000) {
			target.setGold(target.getGold() + target.getGold() / 2);
		}
	}
	
	/**
	 * 修正另外两个目标的可偷取金额
	 * @param targets
	 */
	public static void fixStealsGold(List<ActionTarget> targets) {
		int maxGold = targets.get(0).getGold(); // 正确目标可偷取金额（另外两个目标的金额不能超过该数字）
		ActionTarget target = targets.get(1);
		int val = (int) (target.getGold() - target.getGold() * 0.5f);
		if (val > 0) {
			target.setGold(val);
		}
		if (target.getGold() >= maxGold) {
			target.setGold(CommonUtils.randomInt(stealGoldRegions[0][0], maxGold - 10));
		}
		
		target = targets.get(2);
		val = (int) (target.getGold() - target.getGold() * 0.7f);
		if (val > 0) {
			target.setGold(val);
		}
		if (target.getGold() >= maxGold) {
			target.setGold(CommonUtils.randomInt(stealGoldRegions[0][0], maxGold - 10));
		}
	}
	
	public static long restoreAttackTargetId(Player player, long targetId) {
		if (player.getAdventure().getLastAction() == AdventureType.CAPTURE
				&& RobotUtils.isRobot(targetId)) { // 如果是机器人，需要修正一下id
			List<ActionTarget> list = player.getAdventure().getLastActionTargets();
			if (list != null && list.size() > 0) {
				return list.get(0).getId();
			}
		}
		return targetId;
	}
	
	public static long fakeAttackTargetId(Player player, long targetId) {
		return fakeAttackTargetId(player.getAdventure().getLastAction(), targetId);
	}
	
	public static long fakeAttackTargetId(AdventureType advType, long targetId) {
		if (advType == AdventureType.CAPTURE
				&& RobotUtils.isRobot(targetId)) { // 如果是机器人，统一给客户端同一个id
			return GameStaticConfig.adventureFirstCaptureRobotId;
		}
		return targetId;
	}
	
	/**
	 * 玩家奇遇转到占领功能次数<br/>
	 * @param player
	 * @return -1-未开启, 0~n-累计转到该功能次数（已开启）
	 */
	public static int getAdventureCaptureFuncCount(Player player) {
		int n = player.getPool().getInt(PlayerPool.PROPERTY_ADVENTURE_CAPTURE_FUNC_COUNT, -1);
		if (n == -1) {
//			List<BuildV2> buildings = player.getBuildings2().getBuildings();
//			BuildV2 first = buildings.get(0);
//			if (first.getState() == State.BUILT || first.getState() == State.CAPTURED) { // 开启该功能
//				player.poolSet(PlayerPool.PROPERTY_ADVENTURE_CAPTURE_FUNC_COUNT, 0);
//				return 0;
//			}
			if (player.getLevel() >= GameStaticConfig.adventureCaptureFuncOpenLvl) {
				player.poolSet(PlayerPool.PROPERTY_ADVENTURE_CAPTURE_FUNC_COUNT, 0);
				return 0;
			}
		}
		return n;
	}
	
	public static void addAdventureCaptureFuncCount(Player player, int count) {
		player.poolAdd(PlayerPool.PROPERTY_ADVENTURE_CAPTURE_FUNC_COUNT, count);
	}
	
	static boolean inProtection(Player player, ActionTargetQueue queue) {
		if (player.getLevel() < AdventureUtils.protectionLevel) {
			return true;
		}
		Byte c = queue.getBeTargetCount(player);
		if (c != null && c.byteValue() >= AdventureUtils.maxCountBeTarget) {
			return true;
		}
		return false;
	}
	
	public static boolean canBeSteal(Player source, Player target, ActionTargetQueue queue) {
		return source.getId() != target.getId() && !inProtection(target, queue) && target.getGold() > AdventureUtils.protectionGold;
	}
	
	public static boolean canBeAttack(Player source, Player target, ActionTargetQueue queue) {
		return source.getId() != target.getId() && !inProtection(target, queue) && target.getBuildings2().getLast().canBeAttacked();
	}
	
	public static boolean canBeCapture(Player source, Player target, ActionTargetQueue queue) {
		if (source.getId() == target.getId() || inProtection(target, queue)) {
			return false;
		}
		if (!source.getConquests().isFull()
				&& (
						!App.getApp().getServiceManager().get(RelationService.class).isFriend(source, target)
					|| source.getConquests().sizeByTarget(target) < GameStaticConfig.captureSameFriendBuildMaxCount
					)
				) {
			List<BuildV2> buildings = target.getBuildings2().getBuildings();
			// 第一遍查找已建成但未被占领的建筑
			for (int i = buildings.size() - 1; i >= 0; i--) {
				BuildV2 b = buildings.get(i);
				if (b.getState() == State.BUILT) {
					return true;
				}
			}
			// 第二遍查找被占领的建筑
			for (int i = buildings.size() - 1; i >= 0; i--) {
				BuildV2 b = buildings.get(i);
				if (b.getState() == State.CAPTURED
						&& source.getConquests().find(target.getId(), b.getId()) == null/*未被自己占领*/) {
					return true;
				}
			}
		}
		return false;
	}
	
}
